package com.codi.api.xxljob;

import com.codi.base.util.JsonUtil;
import com.codi.bus.constant.GlobalConstant;
import com.codi.bus.core.resultModel.FundListResult;
import com.codi.bus.core.service.FundInfoService;
import com.codi.bus.core.service.QueryService;
import com.codi.bus.core.service.model.FundModel;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.IJobHandler;
import com.xxl.job.core.handler.annotation.JobHandler;
import com.xxl.job.core.log.XxlJobLogger;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author lvgo
 * @version 1.0
 * @Description: 定时更新基金信息
 * 0 10 1 * * ?
 * @date 18-6-29 下午4:31
 */
@Component
@Slf4j
@JobHandler("updateFundInfoHandler")
public class UpdateFundInfoHandler extends IJobHandler {
    @Resource
    private QueryService queryService;
    @Resource
    private FundInfoService fundInfoService;

    @Override
    public ReturnT<String> execute(String s) throws Exception {
        // 上次的行数
        int lastRowCount = 1;
        FundListResult result = queryService.queryFunds(GlobalConstant.MAX_REQUEST_NUM, 1, lastRowCount, "1", "");
        // 插入数据库
        List<FundModel> insertFundList = result.getFunds();
        if (insertFundList.size() > 0) {
            fundInfoService.insertFundInfo(insertFundList);
        }
        XxlJobLogger.log("insert fund list : {0}", JsonUtil.list2json(insertFundList));
        log.info("==========insert 【{}】 funds===========", insertFundList.size());
        XxlJobLogger.log("UpdateFundInfo-JOB, success");
        return SUCCESS;
    }
}
